package com.grantheath.erebus.model 
{
	import com.grantheath.erebus.model.enum.notifications.ErebusURLNotifications;
	import com.grantheath.erebus.model.enum.notifications.ErebusInitializationNotifications;
	import com.grantheath.erebus.model.enum.names.ErebusProxyNames;
	import com.grantheath.erebus.model.interfaces.IErebusURLProxy;

	import org.puremvc.as3.patterns.proxy.Proxy;

	/**
	 * @author grantheath
	 */
	public class ErebusURLProxy extends Proxy implements IErebusURLProxy 
	{
		private var _URL : String;
		private var _pageTitle : String;
		private var _attributes : Object;

		public function ErebusURLProxy()
		{
			super(ErebusProxyNames.URL_PROXY);
		}
		
		private function setAttributes(URL : String) : void
		{
			var splitURL : Array = URL.split("?");
			var attributes : String = splitURL[1];
			
			if(attributes) {
				var oldAttributes : Object = _attributes;
				var attributesArray : Array = attributes.split("&");
				
				_attributes = {};
				for each(var attribute : String in attributesArray) {
					var splitAttribute : Array = attribute.split("=");
					
					_attributes[splitAttribute[0] as String] = splitAttribute[1];
				}
				if(_attributes != oldAttributes){
					sendNotification(ErebusURLNotifications.URL_ATTRIBUTES_CHANGED, _attributes);
				}
			} else {
				_attributes = null;
			}
		}
		
		//GETTERS AND SETTERS	
		public function get URL() : String
		{
			return _URL;
		}

		public function set URL(URL : String) : void
		{
			if(URL){
				_URL = URL;
			} else {
				_URL = "/";
			}
			setAttributes(_URL);
			sendNotification(ErebusInitializationNotifications.INITIALIZE_QUEUES, _URL);
		}
		
		public function set URLNoTransition(URL : String) : void
		{
			if(URL){
				_URL = URL;
			} else {
				_URL = "/";
			}
			setAttributes(_URL);
			_URL = URL;
		}

		public function get pageTitle() : String
		{
			return _pageTitle;
		}

		public function set pageTitle(pageTitle : String) : void
		{
			_pageTitle = pageTitle;
			sendNotification(ErebusURLNotifications.SET_PAGE_TITLE, _pageTitle);
		}

		public function get attributes() : Object
		{
			return _attributes;
		}
	}
}
